package com.yq.master.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yq.core.base.service.BaseService;
import com.yq.core.entity.SysPowerEntity;
import com.yq.core.service.SysPowerService;
import com.yq.core.util.Page;
import com.yq.core.util.Result;
import com.yq.master.annotation.AzerothAutowired;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 信  息: [yinquan] [2017-03-04] [1.2];
 * 描  述: 信息状态返回类，同意使用此类返回数据
 */
@Controller
@RequestMapping(value = "/sys/menuTop")
public class SysMenuTopController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SysMenuTopController.class);

    @AzerothAutowired
    public SysPowerService sysPowerService;

    protected BaseService getServcie() {
        return sysPowerService;
    }

    protected String getJumpData() {
        String str = "{"
                    +	"insertInit:{"
                    +		"jump:'sys/menu/sys-menu-top-insert'"
                    +	"},"
                    +	"updateInit:{"
                    +		"jump:'sys/menu/sys-menu-top-update'"
                    +	"},"
                    +	"searchInit:{"
                    +		"jump:'sys/menu/sys-menu-top-search',"
                    +		"verify:{},"
                    +	"},"
                +	"}";
        return str;
    }

    /*****************************************************************************************/
    /** 添加顶部菜单初始化 **/
    @RequestMapping(value = "insert",params="_m=init")
    public String insertInit(Model model, ServletRequest request) {
        return "sys/menu/sys-menu-top-insert";
    }

    /** 添加顶部菜单 **/
    @RequestMapping(value = "insert",params="_m=exec")
    @ResponseBody
    public Object insertExec(Model model, ServletRequest request) {
        super.fieldMap.clear();
        super.fieldMap.put("lockStatus", "0");// 0未冻结，1已冻结
        super.fieldMap.put("funType", "0");
        return super.insertExec(model, request);
    }

    /*****************************************************************************************/
    /** 编辑顶部菜单初始化 **/
    @RequestMapping(value = "update",params="_m=init")
    public String updateInit(Model model, ServletRequest request) {
        String uid = request.getParameter("uid");
        if(StringUtils.isNotBlank(uid)) {
            // 编辑初始化页面
            SysPowerEntity sysPowerEntity = sysPowerService.findUniqueById(uid);
            model.addAttribute("obj", sysPowerEntity);
        }
        return "sys/menu/sys-menu-top-update";
    }

    /** 编辑顶部菜单 **/
    @RequestMapping(value = "update",params="_m=exec")
    @ResponseBody
    public Object updateExec(Model model, ServletRequest request) {
        //super.fieldMap.clear();
        return super.updateExec(model, request);
    }

    /*****************************************************************************************/
    /** 删除顶部菜单 **/
    @RequestMapping(value = "delete",params="_m=exec")
    @ResponseBody
    public Object deleteExec(Model model, ServletRequest request) {
        super.fieldMap.clear();
        super.fieldMap.put("deleteStatus", "1");
        return super.updateExec(model, request);
    }

    /*****************************************************************************************/
    /** 添加左侧菜单初始化 **/
    /*@RequestMapping(value = "insertLeft",params="_m=init")
    public String insertLeftInit(Model model, ServletRequest request) {
        return super.insertInit(model, request);
    }*/

    /** 添加左侧菜单 **/
    /*@RequestMapping(value = "insertLeft",params="_m=exec")
    @ResponseBody
    public Object insertLeftExec(Model model, ServletRequest request) {
        super.fieldMap.clear();
        super.fieldMap.put("funType", "1");
        super.fieldMap.put("parentUid", request.getParameter("funTop"));
        return super.insertExec(model, request);
    }*/

    /*****************************************************************************************/
    /** 顶部菜单搜索初始化 **/
    @RequestMapping(value = "search",params="_m=init")
    public String searchInit(Model model, ServletRequest request) {
        return "sys/menu/sys-menu-top-search";
    }

    /** 顶部菜单搜索 **/
    @RequestMapping(value = "search",params="_m=load")
    @ResponseBody
    public Object searchLoad(Model model, ServletRequest request) {
        Page page = super.getPage(request);

        String lockStatus = request.getParameter("lockStatus");
        Map<String, Object> parameters = Maps.newHashMap();
        parameters.put("funType", "0");

        if(StringUtils.isNotBlank(lockStatus) && !lockStatus.equals("-1")) {
            parameters.put("lockStatus", lockStatus);
        }

        page = sysPowerService.pageMenuList(parameters, page);
        //page.setRows(sysMenuValueFilter.valueFilter(page.getRows()));
        return page;

        //return getServcie().paging(parameters, super.getPage(request));
    }

}
